import { reqGetCode, reqGetUserAddress, reqGetUserInfo, reqLogin, reqRegister, reqUserLogout } from '@/api';
import getUserTempId from '@/utils/getUserTempId'

const state = {
  //vuex在项目启动就自动初始化了，从vuex里取比从localStorage取效率高，应该是vuex是在内存了，localStorage在硬盘里
  userTempId : getUserTempId(),
  // code:'',
  // token:'',
  token: localStorage.getItem('token_key') || '',
  userInfo:{},
  userAddressList:[],
};
const mutations = {
  SET_TOKEN(state,token){
    state.token = token;
  },
  SET_USERINFO(state,userInfo) {
    state.userInfo = userInfo;
  },
  SET_RESET(state,){
    state.token = "";
    state.userInfo = {};
  },
  SET_USERADDRESSLIST(state,userAddressList){
    state.userAddressList = userAddressList;
  }
};
const actions = {
  async register(context,userInfo){
    try {
      const result = await reqRegister(userInfo);
      if (result.code === 200) {
        return 'ok'//async函数的promise是成功状态
      } else {
        return Promise.reject( result.message);//async函数的promise是失败状态
      }
    } catch (error) {
      return Promise.reject('注册请求失败');//async函数的promise是失败状态
    }
  },

  async getCode(context,phone){
    try {
      const result = await reqGetCode(phone);
      if (result.code === 200) {
        return result.data;
      } else {
        return Promise.reject('获取验证码失败');
      }
    } catch (error) {
      return Promise.reject('获取验证码请求失败');
    }
  },

  async login({commit},userInfo){
    // console.log(userInfo)
    try {
      const result = await reqLogin(userInfo);
      if (result.code === 200){
        commit('SET_TOKEN',result.data.token);
        localStorage.setItem('token_key',result.data.token);//本地存储
        return 'ok';
      } else {
        return  Promise.reject('请求成功，但是获取token失败')
      }
    } catch (error) {
        return  Promise.reject('登录请求失败');
    }
  },

  async getUserInfo({commit}){
    try {
      const result = await reqGetUserInfo();
      if (result.code === 200) {
        commit('SET_USERINFO',result.data);

        return 'ok';
      } else {
        return Promise.reject('获取用户信息失败');
      }
    } catch (error) {
      return Promise.reject('获取用户信息请求失败');
    }
  },

  reset({commit}){
    commit('SET_RESET');
    localStorage.removeItem('token_key');
  },

  async userLogout({dispatch}){
    try {
      const result = await reqUserLogout();
      if (result.code === 200) {
        //清空token和userInfo
        dispatch('reset');
        return 'ok';
      } else {
        return Promise.reject('退出失败');
      }
    } catch (error) {
      return Promise.reject('退出请求失败');
    }
    

  },

  //获取用户地址
  async getUserAddress({commit}) {
    try {
      const result = await reqGetUserAddress();
      // console.log(result)
      if (result.code === 200) {
        commit('SET_USERADDRESSLIST',result.data);
        alert('获取用户地址成功');
      } else {
        alert('获取用户地址失败');
      }
    } catch (error) {
      alert('获取用户地址请求失败');
    }
  }
};
const getters = {};


export default {
  state,
  mutations,
  actions,
  getters,
  namespaced:true
}